package com.spsoft.vatoutinv.service.vmsenterpriseinfo.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.BeanUtils;
import com.spsoft.core.utils.SecurityUtils;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.vatoutinv.entity.vmsenterpriseinfo.VmsEnterpriseInfoLog;
import com.spsoft.vatoutinv.enums.CommonEnum;
import com.spsoft.vatoutinv.mapper.VmsEnterpriseInfoLogMapper;
import com.spsoft.vatoutinv.model.dto.vmsenterpriseinfo.VmsEnterpriseInfoLogPageDto;
import com.spsoft.vatoutinv.model.vo.vmsenterpriseinfo.VmsEnterpriseInfoLogPageVO;
import com.spsoft.vatoutinv.model.vo.vmsenterpriseinfo.VmsEnterpriseInfoLogVO;
import com.spsoft.vatoutinv.service.vmsenterpriseinfo.VmsEnterpriseInfoLogService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 纳税人基本信息查询记录-企业基本信息、税务信息、财务信息;--主键序列 SEQ_VMS_ENTERPRISE_INFO_LOG 服务实现类
 * </p>
 *
 * @author wangy
 * @since 2023-11-22
 */
@Service
public class VmsEnterpriseInfoLogServiceImpl extends ServiceImpl<VmsEnterpriseInfoLogMapper, VmsEnterpriseInfoLog> implements VmsEnterpriseInfoLogService {

    @Override
    public Result<VmsEnterpriseInfoLogVO> getDetail(String id) {
        if (StringUtil.isEmpty(id)) {
            Result<VmsEnterpriseInfoLogVO> r = new Result<>();
            r.setCode(ResponseEnum.PARAM_VALIDATED_ERROR.getCode());
            r.setMessage(ResponseEnum.PARAM_VALIDATED_ERROR.getMsg());
            return r;
        }
        VmsEnterpriseInfoLog byId = getById(id);
        VmsEnterpriseInfoLogVO vo = new VmsEnterpriseInfoLogVO();
        BeanUtils.copyBeanProp(vo, byId);
        return Result.OK(vo);
    }

    @Override
    public Result<IPage<VmsEnterpriseInfoLogPageVO>> getPage(VmsEnterpriseInfoLogPageDto dto) {
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        LambdaQueryWrapper<VmsEnterpriseInfoLog> lq = new LambdaQueryWrapper<VmsEnterpriseInfoLog>()
                .like(StringUtil.isNotEmpty(dto.getCreateUsername()), VmsEnterpriseInfoLog::getCreateUsername, dto.getCreateUsername())
                .eq(StringUtil.isNotEmpty(dto.getOperateStatus()), VmsEnterpriseInfoLog::getOperateStatus, dto.getOperateStatus())
                .and((StringUtil.isNotEmpty(dto.getStartTime()) || StringUtil.isNotEmpty(dto.getEndTime())), wrapper -> wrapper
                        .apply(StringUtil.isNotEmpty(dto.getStartTime()), "CREATE_TIME >= to_timestamp('" + dto.getStartTime() + "', 'YYYY-MM-DD HH24:MI:SS')")
                        .apply(StringUtil.isNotEmpty(dto.getEndTime()), "CREATE_TIME <= to_timestamp('" + dto.getEndTime() + "', 'YYYY-MM-DD HH24:MI:SS')")
                )
                .eq(VmsEnterpriseInfoLog::getDeptCode, SecurityUtils.getDeptCode())
                .orderByDesc(VmsEnterpriseInfoLog::getCreateTime);
        IPage<VmsEnterpriseInfoLogPageVO> page = this.page(new Page<>(pageNo, pageSize), lq).convert(item -> {
            VmsEnterpriseInfoLogPageVO vo = new VmsEnterpriseInfoLogPageVO();
            BeanUtils.copyBeanProp(vo, item);
            // 调用乐企结果为成功，不返回前端执行结果，防止operateMsg字段过长
            if (CommonEnum.LEQI_RETURN_CODE_SUCCESS.getKey().equals(item.getOperateStatus())) {
                vo.setOperateMsg(null);
            }
            return vo;
        });
        return Result.OK(page);
    }
}
